//
//  SettingView.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "SettingView.h"
#import "SettingCell.h"
#import "Cache.h"

@interface SettingView ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_pictures;
    NSArray *_titles;
}
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

static NSString *cellID = @"cell";

@implementation SettingView

-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    self.frame = newSuperview.bounds;
    self.toolBar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(remove)];
    [self.toolBar addGestureRecognizer:tap];
    
    _pictures = @[@"pictorial.png",@"wallpaper.png",@"my_download.png",@"clear.png"];
    _titles = @[@"画报集",@"壁纸集",@"我的下载",@"清除缓存"];
    
    self.tableView.layer.cornerRadius = 10;
    self.tableView.clipsToBounds = YES;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}
-(void)remove{
    [_delegate removeSetting];
}
#pragma mark - TableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_delegate moveToController:indexPath.row];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [SettingCell nibInitializtion];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.picture = _pictures[indexPath.row];
    if(indexPath.row == 3){
        dispatch_queue_t queue = dispatch_queue_create("thread", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSString *cacheSize = [Cache cacheSize];
            dispatch_async(dispatch_get_main_queue(), ^{
                UILabel *cacheLabel = [UILabel labelWithText:cacheSize fontSize:12 frame:CGRectMake(0, 24, self.tableView.frame.size.width - 5, 20) color:[UIColor blackColor] textAlignment:2];
                [cell addSubview:cacheLabel];
            });
        });

    }
    cell.title = _titles[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
@end
